package com.tourism.util.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * @Description
 * @Author z
 * @Date 2021-03-05 20:33
 */
public class Response<T> {

    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 状态编码
     */
    private String code;

    /**
     * 处理结果描述
     */
    private String msg;

    /**
     * 返回的对象信息
     * 该注解是不参与序列化，为null的时候不传递给前台
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String isCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success() {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg("success");
        response.setData(null);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg(msg);
        response.setData(null);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(T obj) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg("success");
        response.setData(obj);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> success(T obj, String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(true);
        response.setMsg(msg);
        response.setData(obj);
        return response;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> Response<T> error(T obj, String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setMsg(msg);
        response.setData(obj);
        return response;
    }

    /**
     * 处理异常返回
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> error(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setMsg(msg);
        response.setData(null);
        return response;
    }

    /**
     * 处理警告返回
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> warn(String msg) {
        Response<T> response = new Response<T>();
        response.setSuccess(false);
        response.setCode("warn");
        response.setMsg(msg);
        response.setData(null);
        return response;
    }
}
